﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Windows;
using System.Windows.Media;
using FinPlusInterfaces;
using FinPlusAssembler;

namespace FinPlusCompView
{
    public partial class FinPlusTransform : FinPlusControl
    {
        [State(Test = true)]
        public override bool IsValid { get; set; }
        [State(Save = true, Test=true)]
        public double ScaleValue { get { return _scaleValue; } set { _scaleValue = value; Transform(); } }
        [State(Save = true, Test = true)]
        public double RotateAngle { get { return _rotateAngle; } set { _rotateAngle = value; Transform(); } }

        private FinPlusWindow _window;
        private FinPlusControl _view;
        private double _rotateAngle, _scaleValue;
        private double _height, _width;

        //construct
        public FinPlusTransform(FinPlusWindow window, FinPlusControl view)
        {
            _window = window;
            _view = view;
            _height = _window.Height;
            _width = _window.Width;
            
            InitializeComponent();
            IsValid = true;
        }
    
        //common control interface 
        public override void CmdExecute(IPod command) 
        {
            try
            {
                CmdCapture(command);

                switch ((Cmds)Enum.Parse(typeof(Cmds), command.Name, true))
                {
                    case Cmds.Scale: Scale(); break;
                    case Cmds.Rotate: Rotate(); break;
                    case Cmds.Load: Transform(); break;
                    case Cmds.Clear: Clear(); break;
                    default: throw new Exception(string.Format("cmd not recognised {0}", command.Name));
                }
            }
            catch (CmdExecuteException e)
            {
                Level.Error.Log(Id, Config, e.Message, e);
            }
        }

        //private
        private void Scale()
        {
            _scaleValue = scaleSlider.Value;
            Transform();
        }

        private void Rotate()
        {
            var height = _window.Height;
            var width = _window.Width;
            _rotateAngle = _rotateAngle + 90 % 360;
            Transform();
            _window.Width = height;
            _window.Height = width;
        }

        private void Transform()
        {
            var group = new TransformGroup();
            group.Children.Add(new RotateTransform(_rotateAngle));
            group.Children.Add(new ScaleTransform(_scaleValue, _scaleValue));
            _view.LayoutTransform = group;
            _view.UpdateLayout();
        }

        private void Clear()
        {
            _scaleValue = 1;
            _rotateAngle = 0;
            _window.Height =  _height;
            _window.Width = _width;
            _view.LayoutTransform = null;
            _view.UpdateLayout();
        }

        //cmds
        private enum Cmds { Scale, Rotate, Load, Clear }

        private void Slider_Changed(object s, RoutedPropertyChangedEventArgs<double> a) { CmdRouter(Cmds.Scale.ToString()); }
        private void Rotate_Click(object s, RoutedEventArgs a) { CmdRouter(Cmds.Rotate.ToString()); }
        private void Control_Loaded(object s, RoutedEventArgs a) { CmdRouter(Cmds.Load.ToString()); }
        private void Clear_Click(object s, RoutedEventArgs a) { CmdRouter(Cmds.Clear.ToString()); }
    }
}














































































































































































//Copyright2013<jnrton>AllRightsReserved 
//Contact j.nrton@finpus